/**********************************************************************\

 Copyright 2012 Diggory Blake

 This file is part of klip-engine

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

\**********************************************************************/

#include "kbasiccontroller.h"
#include "kactor.h"

void kBasicController::onActorAdded(kActor* actor) {
	kController::onActorAdded(actor);

	setActorData(actor, new kActorData());
	setTransform(actor, actor->getTransform());
}
void kBasicController::onActorRemoved(kActor* actor) {
	delete (kActorData*)getActorData(actor);
	setActorData(actor, 0);

	kController::onActorRemoved(actor);
}
void kBasicController::updateTransform(kActor* actor, const kActorData* data) {
	kMat3x3 mat;
	compose(&mat, data->position, data->scale, data->angle);
	setActorTransform(actor, mat);
}
void kBasicController::setTransform(kActor* actor, const kMat3x3& transform) {
	kActorData* data = (kActorData*)getActorData(actor);

	decompose(transform, &data->position, &data->scale, &data->angle);
	updateTransform(actor, data);
}
void kBasicController::setTransform(kActor* actor, const kVec2& position, const kVec2& scale, float angle) {
	kActorData* data = (kActorData*)getActorData(actor);

	data->position = position;
	data->scale = scale;
	data->angle = angle;
	updateTransform(actor, data);
}
void kBasicController::setPosition(kActor* actor, const kVec2& position) {
	kActorData* data = (kActorData*)getActorData(actor);

	data->position = position;
	updateTransform(actor, data);
}
void kBasicController::setScale(kActor* actor, const kVec2& scale) {
	kActorData* data = (kActorData*)getActorData(actor);

	data->scale = scale;
	updateTransform(actor, data);
}
void kBasicController::setAngle(kActor* actor, float angle) {
	kActorData* data = (kActorData*)getActorData(actor);

	data->angle = angle;
	updateTransform(actor, data);
}
const kVec2& kBasicController::getPosition(kActor* actor) {
	kActorData* data = (kActorData*)getActorData(actor);

	return data->position;
}
const kVec2& kBasicController::getScale(kActor* actor) {
	kActorData* data = (kActorData*)getActorData(actor);

	return data->scale;
}
float kBasicController::getAngle(kActor* actor) {
	kActorData* data = (kActorData*)getActorData(actor);

	return data->angle;
}
kBasicController::kBasicController() {
}
